/*
 * @Author: zhoujinlong 765637726@qq.com
 * @Date: 2024-02-26 09:08:40
 * @LastEditors: zhoujinlong 765637726@qq.com
 * @LastEditTime: 2024-03-19 10:51:48
 * @FilePath: \pingxi\src\router\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
import routes from "./modules/router";
import http from "@/http";

import { storeToRefs } from "pinia"; // cnpm i pinia --save  2.1.3
import { useCounterStore } from "@/current";

const router = createRouter({
  history: createWebHashHistory(),
  routes,
  // routes: [...routes, ...asyncRouter],
});
/**
 * @description:  页面鉴权
 * @param {*} param1
 * @return {*}
 */
router.beforeEach((to, from, next) => {
  // console.log(to.matched);
  // console.log(to, from, next);

  if (to.name !== "login" && !localStorage.getItem("token")) {
    next({ name: "login" });
  } else {
    if (to.matched.length == 0) {
      next({ path: "/error/404" });
    } else {
      const store = useCounterStore();
      const { get_router } = store;  

      if (to.path != "/login" && !localStorage.getItem("token")) {
        get_router();
      }

      next();
    }
  }
});

export default router;

